package com.sgb.kjwl.view.ui.activity.self.publish;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityAddBrand3Binding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.view.event.self.GoodsEvent;
import com.sgb.kjwl.viewmodel.self.publish.AddBrandModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;


import javax.inject.Inject;

import static com.swgk.core.event.EventConstant.EVENT_BRAND_ADD;

public class AddBrandActivity extends AppActivity{
    private ActivityAddBrand3Binding binding;
    private String typeId;
    private GoodsEvent goodsEvent;
    @Inject
    AddBrandModel viewModel;

    public static void start(Context context, String typeId) {
        Intent intent = new Intent();
        intent.putExtra("typeId", typeId);
        intent.setClass(context, AddBrandActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    public static void start(Context context, Intent extras) {
        Intent intent = new Intent();
        intent.setClass(context, AddBrandActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        if (extras != null) {
            intent.putExtras(extras);
        }
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder()
                .appComponent(appComponent)
                .selfModule(new SelfModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    protected void initView() {
        typeId = getIntent().getStringExtra("typeId");
        viewModel.brandEntity3.get().setCid3(typeId);
        viewModel.brandEntity3.get().setId("");
        binding = DataBindingUtil.setContentView(AddBrandActivity.this, R.layout.activity_add_brand3);
        binding.setViewModel(viewModel);
        binding.backView.setOnClickListener(this);
        binding.nextView.setOnClickListener(this);
        binding.nS.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        binding.nS.setFocusable(true);
        binding.nS.setFocusableInTouchMode(true);
        binding.nS.setOnTouchListener((v, event) -> {
            v.requestFocusFromTouch();
            return false;
        });
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void init() {
    }



    public void showSuccess() {
        goodsEvent = new GoodsEvent();
        goodsEvent.setMessageTag(EVENT_BRAND_ADD);
        EventBus.getDefault().post(goodsEvent);
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.next_view:
                if (TextUtils.isEmpty(binding.itemTvName.getContent())) {
                    MToast.showToast(AddBrandActivity.this, "请输入品牌名称");
                    return;
                }
                viewModel.brandEntity3.get().setMark(binding.etMark.getText().toString().trim());
                viewModel.saveBrand();
                break;
            case R.id.back_view:
                finish();
                break;
        }
    }
}

